// 自己选择的路，跪着也要走完。朋友们，虽然这个世界日益浮躁起来,只
// 要能够为了当时纯粹的梦想和感动坚持努力下去，不管其它人怎么样,我
// 们也能够保持自己的本色走下去。                      ——AKPower
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;

string s[60];
vector<string> s1;
vector<string> s2;
map<string ,ll > mp;
map<string ,bool > vis;

ll flag;
int main()
{
    IOS;
    ll n;
    cin>>n;
    for(ll i=0;i<n;i++){
        cin>>flag>>s[i];
        mp[s[i]]=i+1;
        if(!flag)s1.push_back(s[i]);
        else s2.push_back(s[i]);
    }    
    bool ch;
    for(ll i=0;i<n;i++){
        if(vis[s[i]])continue;
        ch=false;
        vis[s[i]]=true;
        if(s[i]==s1[0]){
            string temp2=s2[s2.size()-1];
            string temp1=s[i];
            if(mp[temp1]>mp[temp2]){swap(temp1,temp2);ch=true;}
            cout<<temp1<<" "<<temp2<<endl;
            if(ch)vis[temp1]=true;
            else vis[temp2]=true;
            s1.erase(s1.begin());
            s2.erase(s2.end()-1);
        }
        else {
            string temp2=s1[s1.size()-1];
            string temp1=s[i];
            if(mp[temp1]>mp[temp2]){swap(temp1,temp2);ch=true;}
            cout<<temp1<<" "<<temp2<<endl;
            if(ch)vis[temp1]=true;
            else vis[temp2]=true;
            s2.erase(s2.begin());
            s1.erase(s1.end()-1);
        }
    }
    getchar();
    getchar();
    return 0;
}
